package com.gitee.ricesugar.admin.module;

import cn.hutool.core.util.IdUtil;
import com.gitee.ricesugar.entity.Admin;
import com.gitee.ricesugar.exception.ErrorException;
import com.gitee.ricesugar.mapper.AdminMapper;
import com.gitee.ricesugar.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;

public class AddAdmin extends JPanel {
    private AdminMapper adminMapper;
    private SqlSession sqlSession;

    public AddAdmin() throws IOException {
        setLayout(new GridBagLayout());

        createSqlSession();

        // 创建 GridBagConstraints 对象
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);  // 设置组件之间的间距

        // 添加标题
        JLabel titleLabel = new JLabel("添加管理员");
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 30));
        gbc.gridx = 0;
        gbc.gridy = 0;
        add(titleLabel, gbc);

        // 添加标签和输入框

        // username
        gbc.gridx = 0;
        gbc.gridy = 2;
        add(CreateLabel("用户名:"), gbc);
        gbc.gridx = 1;
        gbc.gridy = 2;
        JTextField userName = CreateTextField();
        add(userName, gbc);

        // password
        gbc.gridx = 0;
        gbc.gridy = 3;
        add(CreateLabel("密码:"), gbc);
        gbc.gridx = 1;
        gbc.gridy = 3;
        JTextField passWord = CreateTextField();
        add(passWord, gbc);

        // name
        gbc.gridx = 0;
        gbc.gridy = 4;
        add(CreateLabel("姓名:"), gbc);
        gbc.gridx = 1;
        gbc.gridy = 4;
        JTextField name = CreateTextField();
        add(name, gbc);

        // phone
        gbc.gridx = 0;
        gbc.gridy = 7;
        add(CreateLabel("电话:"), gbc);
        gbc.gridx = 1;
        gbc.gridy = 7;
        JTextField phone = CreateTextField();
        add(phone, gbc);

        // email
        gbc.gridx = 0;
        gbc.gridy = 8;
        add(CreateLabel("邮箱:"), gbc);
        gbc.gridx = 1;
        gbc.gridy = 8;
        JTextField email = CreateTextField();
        add(email, gbc);

        // 添加按钮
        gbc.gridx = 1;
        gbc.gridy = 10;
        JButton addButton = CreateButton("添加");
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Admin admin = new Admin();
                String uuid = Integer.toString(IdUtil.fastSimpleUUID().hashCode());
                admin.setUid(uuid);
                admin.setUsername(userName.getText());
                admin.setPassword(passWord.getText());
                admin.setName(name.getText());
                admin.setPhone(phone.getText());
                admin.setEmail(email.getText());
                try {
                    adminMapper.add(admin);
                    sqlSession.commit();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                System.out.println("addButton clicked");
            }
        });
        add(addButton, gbc);

        // 清空按钮
        gbc.gridx = 2;
        gbc.gridy = 10;
        JButton clearButton = CreateButton("清空");
        clearButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                userName.setText("");
                passWord.setText("");
                name.setText("");
                phone.setText("");
                email.setText("");
            }
        });
        add(clearButton, gbc);
    }

    private JLabel CreateLabel(String name) {
        JLabel jLabel = new JLabel(name);
        jLabel.setFont(new Font("微软雅黑", Font.BOLD, 15));
        return jLabel;
    }

    private JTextField CreateTextField() {
        JTextField jTextField = new JTextField(20);
        jTextField.setFont(new Font("微软雅黑", Font.BOLD, 15));
        return jTextField;
    }

    private JButton CreateButton(String name) {
        JButton jButton = new JButton(name);
        jButton.setFont(new Font("微软雅黑", Font.BOLD, 20));
        return jButton;
    }

    private void createSqlSession() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession = sqlSessionFactory.openSession();
        adminMapper = sqlSession.getMapper(AdminMapper.class);
    }
}
